import Pyro4
import logging as l
import sys
import threading

l.basicConfig(level=l.DEBUG)
#Pyro4.config.COMMTIMEOUT = 10

class Debugger(object):
    def __init__(self):
        l.debug("Node created")
        self.nodes = {}
    
    def addNode(self, nodeName, nodeUri):
        print "Pridavam node {0}".format(nodeName)
        self.nodes[nodeName] = Pyro4.Proxy(nodeUri)
    
    def run(self):
        def callMethod():
            while True:
                name = raw_input("name: ")
                method = raw_input("Metoda: ")
                if method == "printStatus":
                    self.nodes[name].printStatus()
                
        thread=threading.Thread(target=callMethod)
        thread.setDaemon(True)
        thread.start()

def main():
    dbg = Debugger()
    daemon=Pyro4.Daemon()
    dbg_uri=daemon.register(dbg)
    ns = Pyro4.locateNS()
    ns.register("dbg", dbg_uri)
    for nodeName, node_uri in ns.list(prefix="node.").items():
        dbg.addNode(nodeName, node_uri)    

    dbg.run()
    daemon.requestLoop()

if __name__ == "__main__":
    main()
